package com.zimin.mapper.injector;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.zimin.common.utils.BeanUtils;
import org.apache.ibatis.annotations.Param;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @author zimin
 * @since 2022/1/19 2:06 下午
 */
public interface InjectBaseMapper<T> extends BaseMapper<T> {

    /**
     * <p>自定义查询返回类型</p>
     *
     * @param id
     * @param v
     * @return V
     * @since 2022-01-21 14:30:14
     */
    default <V> V selectById(@Param(value = "id") Serializable id, Class<V> v) {
        T t = selectById(id);
        try {
            V v1 = v.getDeclaredConstructor().newInstance();
            BeanUtils.copyProperties(t, v1);
            return v1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * <p>根据id批量查询自定义返回类型</p>
     *
     * @param idList id 列表
     * @param v
     * @return java.util.List<V>
     * @since 2022-01-24 16:43:39
     */
    default <V> List<V> selectBatchIds(@Param(Constants.COLL) Collection<? extends Serializable> idList,
                                       Class<V> v) {
        List<T> t = selectBatchIds(idList);
        return BeanUtils.listEntityToView(t,v);
    }


    /**
     * <p>条件查询  自定义返回类型</p>
     *
     * @param queryWrapper 查询条件
     * @param v 转换类型
     * @return java.util.List<V>
     * @author zimin
     * @since 2022-01-24 16:56:33
     */
    default <V> List<V> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper, Class<V> v) {
        List<T> t = selectList(queryWrapper);
        return BeanUtils.listEntityToView(t,v);
    }
}
